<?php
/**
 * 系統名稱: Lotto 系統
 * 檔案說明: 專案管理-修改
 * $Author$
 * $Id$
 *
 */
set_time_limit(0);
$intSuppleID = (int)$_data["SuppleID"];
$intBT = (int)$_data["Bet_Type"];
$intMemID = (int)$_data["MemID"];
$intGID = (int)$_data["GambleID"];
$flOdds = (float)$_data["Odds"];
$flOdds2 = (float)$_data["Odds2"];
$flStakeMoney = (float)$_data["StakeMoney"];
$isSpecial = trim($_data["isSpecial"]);
$strSNo = trim($_data["SerialNo"]);
$intSpecialNumType = (int)$_data["SpecialNumType"];
$strChoiceNumber = trim($_data["ChoiceNumber"]);

//檢驗傳入資料
if($intBT < 1 || $intGID < 1 || $intLType != 3 || $intLottoID < 1 || $flOdds <= 0 || $flStakeMoney <= 0 || $intMemID <= 0 || $strSNo == ""){
	$GLOBALS["errormessage"] = "Data Error!!";
	$GLOBALS["errorhidden"] = "\nBetType:".$intBT."\nGambleID:".$intGID."\nLottoID:".$intLottoID."\nflOdds:".$flOdds."\nStakeMoney:".$flStakeMoney."\nMemID:".$intMemID."\nSerialNo:".$strSNo."\nLine:".__LINE__."\nPage:".__FILE__;
	ErrMsgOutputHTML("Error", "", 1);
}

//取得補貨帳號資料
$strSQL = "select * from `bet_Member` where MemberGroup = 21 and isLock = 0 and ID = ".(string)$intMemID." limit 0,1";
if(!$RS = @PMA_mysql_query($strSQL, $$_Conn)){
	$GLOBALS["errorhidden"] = "Find User Data Error!";
	$GLOBALS["errorhidden"] = "SQL: ".$strSQL.", \nError: ".PMA_mysql_error($$_Conn).", \nLine: ".__LINE__.", \nPage: ".__FILE__;
	ErrMsgOutputHTML("Error", "", 1);
}

if(@PMA_mysql_num_rows($RS) == 0){
	$GLOBALS["errorhidden"] = "Can not Find User Data!!";
	$GLOBALS["errorhidden"] = "SQL: ".$strSQL.", \nError: ".PMA_mysql_error($$_Conn).", \nLine: ".__LINE__.", \nPage: ".__FILE__;
	ErrMsgOutputHTML("Error", "", 1);
}
$objMem = PMA_mysql_fetch_object($RS);

//找出會員退水
$strSQL = "select * from `bet_Commission` where MemID = ".$objMem -> ID." and Gamble_Type = ".$objMem -> GambleType." and Bet_Type = '".$intBT."' and LottoID = '".$intLottoID."' limit 0, 1";
if(!$RS = PMA_mysql_query($strSQL, $$_Conn)){
	$GLOBALS["errormessage"] = "Find Commission Data Error!";
	$GLOBALS["errorhidden"] = "Error: ".PMA_mysql_error($$_Conn)."\nLine:".__LINE__."\nPage:".__FILE__;
	ErrMsgOutputHTML("Error", "", 1);
}
if(PMA_mysql_num_rows($RS) == 0){
	$GLOBALS["errormessage"] = "Can not Find Commission Data!!";
	$GLOBALS["errorhidden"] = "Error: ".PMA_mysql_error($$_Conn)."\nSQL:".$strSQL."\nLine:".__LINE__."\nPage:".__FILE__;
	ErrMsgOutputHTML("Error", "", 1);
}
$Rows = @mysql_fetch_object($RS);
$intComm = $Rows -> Commission;


//組合陣列 for bet_BetListContent 用
$arrWCont["GameDate_x"] = $_data["GameDate"];
$arrCheckKey = split(" ",microtime());
$arrWCont["checkkey_x"] = str_replace("0.", $arrCheckKey[1]."-", $arrCheckKey[0]);
$arrWCont["GambleType"] = $objMem -> GambleType;
$arrWCont["MemAccount_x"] = $objMem -> Account;
$arrWCont["isBlack"] = $objMem -> isBlack;
$isJoinWager = false;
$ChoiceGroup = 1;
switch($intBT){
	default:
		//取得下注內容
		$strWagerConditions = getWagerConditions($intLType, $intBT, $_data["Number"], $flOdds, $strSNo);
		$arrWList["Number"] = $_data["Number"];
		break;
}
$arrWCont["Content_x"] = $strWagerConditions;

$flMemPayOff = ($flStakeMoney*($flOdds - 1));
$flMemERate = (empty($_ECRate[$objMem -> ERID]["Rate"]))?1:$_ECRate[$objMem -> ERID]["Rate"];
//組合陣列 for bet_BetList 用
$arrWList["LottoID"] = $intLottoID;
$arrWList["Bet_Type"] = $intBT;
$arrWList["GambleID"] = $intGID;
$arrWList["OddsID"] = -999;
$arrWList["Odds"] = $flOdds;
$arrWList["MemID"] = $objMem -> ID;
$arrWList["MemExchangeRate"] = $flMemERate;
$arrWList["MemCurrency"] = $objMem -> ERID;
$arrWList["MemStake"] = $flStakeMoney*$ChoiceGroup*-1;
$arrWList["MemPayOff"] = $flMemPayOff*-1;
$arrWList["MemCommission"] = 1 - ($intComm / 100);
$arrWList["SubID"] = $objMem -> ID;
$arrWList["SubCommission"] = 1 - ($intComm / 100);
$arrWList["SubRate"] = 1;
$arrWList["MainID"] = $objMem -> ID;
$arrWList["MainCommission"] = 1 - ($intComm / 100);
$arrWList["MainRate"] = 1;
$arrWList["StockID"] = $objMem -> ID;
$arrWList["StockCommission"] = 1 - ($intComm / 100);
$arrWList["StockRate"] = 1;
$arrWList["DirectorID"] = $objMem -> ID;
$arrWList["DirectorCommission"] = 1 - ($intComm / 100);
$arrWList["DirectorRate"] = 1;
$arrWList["PayupDate_x"] = $_data["GameDate"];
$arrWList["WagerType"] = 1;

//外調下注
if($_data["isGetSuppleOdds"] == "Y")$arrWList["isPayOff"] = -2;

PMA_mysql_query("BEGIN", $_ConnMaster);
//寫入注單
$arrBetData = InsertWagerToDB($arrWCont, $arrWList);
if($arrBetData["isError"]){
	PMA_mysql_query("ROLLBACK", $_ConnMaster);
	$GLOBALS["errormessage"] = $arrBetData["ErrorMessage"];
	$GLOBALS["errorhidden"] = $arrBetData["ErrorHiddenMessage"];
	AddData_Log($_SESSION["Admin_Info"] -> ID,"補貨下注失敗", $arrBetData["LogData"]."\n".$GLOBALS["errorhidden"]);
	ErrMsgOutputHTML("Error", "", 1);
}

//更改 TodayGame 狀態
if($_data["isGetSuppleOdds"] != "Y"){
	$flStakeMoney = $flStakeMoney * $flMemERate*$ChoiceGroup;
	$flMemPayOff *= $flMemERate;
	$flComm = $flStakeMoney*$arrWList["DirectorCommission"];
	$strOhterCase = " tolSuppleWager = 1 + tolSuppleWager, tolSuppleStake = tolSuppleStake + ".$flStakeMoney.", tolSupplePayOff = tolSupplePayOff + ".$flMemPayOff.", tolSuppleCommission = tolSuppleCommission + ".$flComm;
	if(!UpdateTodayGameOhterCase($intGID, $intLottoID, $intBT, $_data["Number"], $strOhterCase)){
		PMA_mysql_query("ROLLBACK", $_ConnMaster);
		AddData_Log($_SESSION["Admin_Info"] -> ID,"補貨下注失敗", $arrBetData["LogData"]."\n".$GLOBALS["errorhidden"]);
		ErrMsgOutputHTML("Error", "", 1);
	}
}else{
	//設定注單排隊
	if(!setWagerOrder($arrBetData["BetID"], 0)){
		PMA_mysql_query("ROLLBACK", $_ConnMaster);
		$GLOBALS["errormessage"] = $arrBetData["ErrorMessage"];
		$GLOBALS["errorhidden"] = $arrBetData["ErrorHiddenMessage"];
		AddData_Log($_SESSION["Admin_Info"] -> ID, "補貨下注失敗", join("\n", $arrLogData)."\n".$GLOBALS["errorhidden"]);
		ErrMsgOutputHTML("Error", "", 1);
	}
}
if(!PMA_mysql_query("COMMIT", $_ConnMaster)){
	if(!rmWagerOrder($arrBetData["BetID"])){
		AddData_Log($_SESSION["Admin_Info"] -> ID, "移除下注排序失敗", join("\n", $arrLogData)."\n".$GLOBALS["errorhidden"]);
		ErrMsgOutputHTML("Error", "", 1);
	}
}

//外調下注
if($_data["isGetSuppleOdds"] == "Y"){
	$strNumber = $arrWList["Number"];
	$arrResult = SuppleWager($arrWList["LottoID"], $intLType, $arrWList["Bet_Type"], $strNumber, $flStakeMoney, $_data["OddsID"], $arrWList["Odds"], $_data["OddsID2"], $arrWList["Odds2"], $strChoiceNumber, $arrCheckKey[0],  $arrCheckKey[1]+1, $arrBetData["BetID"]);
	if($arrResult["isWager"] == false){
		if(!rmWagerOrder($arrBetData["BetID"])){
			AddData_Log($_SESSION["Admin_Info"] -> ID, "移除下注排序失敗", join("\n", $arrLogData)."\n".$GLOBALS["errorhidden"]);
		}
		ErrMsgOutputHTML("Error", "", 1);
	}else{
		PMA_mysql_query("BEGIN", $_ConnMaster);
		$arrSQL[0] = "update `bet_BetList` set isPayOff = 0 where ID in (".$arrBetData["BetID"].")";
		$arrSQL[1] = "update `bet_BetListTmp` set isPayOff = 0 where ID in (".$arrBetData["BetID"].")";
		foreach($arrSQL as $strSQL){
			if(!PMA_mysql_query($strSQL, $_ConnMaster)){
				PMA_mysql_query("ROLLBACK", $_ConnMaster);
				$GLOBALS["errormessage"] = "System Error!!";
				$GLOBALS["errorhidden"] = "\n Error: ".PMA_mysql_error($_ConnMaster).",\nSQL: ".$strSQL.",\nLine: ".__LINE__.",\nPage: ".__FILE__;
				AddData_Log($_SESSION["Admin_Info"] -> ID,"外調補貨下注失敗", $arrBetData["LogData"]."\n".$GLOBALS["errorhidden"]);
				if(!rmWagerOrder($arrBetData["BetID"])){
					AddData_Log($_SESSION["Admin_Info"] -> ID, "移除下注排序失敗", join("\n", $arrLogData)."\n".$GLOBALS["errorhidden"]);
				}
				ErrMsgOutputHTML("Error", "", 1);
			}
		}
		$flStakeMoney = $flStakeMoney * $flMemERate*$ChoiceGroup;
		$flMemPayOff *= $flMemERate;
		$flComm = $flStakeMoney*$arrWList["DirectorCommission"];
		$strOhterCase = " tolSuppleWager = 1 + tolSuppleWager, tolSuppleStake = tolSuppleStake + ".$flStakeMoney.", tolSupplePayOff = tolSupplePayOff + ".$flMemPayOff.", tolSuppleCommission = tolSuppleCommission + ".$flComm;
		if(!UpdateTodayGameOhterCase($intGID, $intLottoID, $intBT, $_data["Number"], $strOhterCase)){
			PMA_mysql_query("ROLLBACK", $_ConnMaster);
			AddData_Log($_SESSION["Admin_Info"] -> ID,"外調補貨下注失敗", $arrBetData["LogData"]."\n".$GLOBALS["errorhidden"]);
			if(!rmWagerOrder($arrBetData["BetID"])){
				AddData_Log($_SESSION["Admin_Info"] -> ID, "移除下注排序失敗", join("\n", $arrLogData)."\n".$GLOBALS["errorhidden"]);
			}
			ErrMsgOutputHTML("Error", "", 1);
		}
		PMA_mysql_query("COMMIT", $_ConnMaster);
	}
}
if(!rmWagerOrder($arrBetData["BetID"])){
	AddData_Log($_SESSION["Admin_Info"] -> ID, "移除下注排序失敗", join("\n", $arrLogData)."\n".$GLOBALS["errorhidden"]);
}

AddData_Log($_SESSION["Admin_Info"] -> ID,"補貨下注成功 ", $arrBetData["LogData"]);

if($intBT >= 1 && $intBT <= 13){
	/*=================================================================*/
	header("Cache-Control: no-store, no-cache, must-revalidate");	  // HTTP/1.1
	header("Cache-Control: post-check=0, pre-check=0", false);
	header("Pragma: no-cache");															  // HTTP/1.0
	?><meta http-equiv="Content-Type" content="text/html; charset=utf-8">
	<script language="JavaScript">
	<!--
	parent.ReChangeBet();
	//-->
	</script><?php
	exit();
}
?>